<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="年级名称" prop="gradeName">
        <el-input
          v-model="queryParams.gradeName"
          placeholder="请输入年级名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入学年份" prop="enrolYear">
        <el-date-picker
          v-model="queryParams.enrolYear"
          type="year"
          value-format="yyyy"
          placeholder="请选择入学年份"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:school-grade:create']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:school-grade:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <!-- <dict-tag> 是自定义组件的标签名，类似于HTML中的标签。

:type="DICT_TYPE.GRADE_STAGE" 是传递字符串类型的 DICT_TYPE.GRADE_STAGE 给自定义组件 dict-tag 的属性 type。:前缀表示传递动态的数据。

:value="scope.row.stage" 是传递 scope.row.stage 的值给自定义组件 dict-tag 的属性 value。:前缀表示传递动态的数据。 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="年级名称" align="center" prop="gradeName" />
      <el-table-column label="入学年份" align="center" prop="enrolYear" />

      <el-table-column label="教育阶段" align="center" prop="stage">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.GRADE_STAGE" :value="scope.row.stage" />
        </template>
      </el-table-column>
      <el-table-column label="所在年级" align="center" prop="inGrade">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.GRADE_LEVEL" :value="scope.row.inGrade" />
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="principal" />
      <el-table-column label="联系电话" align="center" prop="mobile" />
      <el-table-column label="年级状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="年级类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.GRADE_STATUS" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="学制" align="center" prop="lengthSchool" />
      <!-- <el-table-column label="上级机构" align="center" prop="deptId" /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:school-grade:update']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:school-grade:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年级名称" prop="gradeName">
          <el-input v-model="form.gradeName" placeholder="请输入年级名称" />
        </el-form-item>
        <el-form-item label="入学年份" prop="enrolYear">
          <el-date-picker
            v-model="form.enrolYear"
            type="year"
            placeholder="请选择入学年份"
            style="width: 100%"
            value-format="yyyy"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="教育阶段" prop="stage">
          <el-select v-model="form.stage" placeholder="请选择教育阶段">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.GRADE_STAGE)"
              :key="dict.value"
              :value="dict.value"
              :label="dict.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所在年级" prop="inGrade">
          <el-select v-model="form.inGrade" placeholder="请选择所在年级">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.GRADE_LEVEL)"
              :key="dict.value"
              :value="parseInt(dict.value)"
              :label="dict.label"
            />
          </el-select>
        </el-form-item>
        <el-row>
          <el-col :span="16" :xs="24">
            <el-form-item label="负责人" prop="principal">
              <el-select
                v-model="form.principal"
                placeholder="请选择负责人"
                @visible-change="userListChange"
              >
                <el-option
                  v-for="item in userList"
                  :key="item.value"
                  :value="item.nickname"
                  :label="item.nickname"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" :xs="24" style="padding-top: 10px">
            <el-link
              v-show="linkShow"
              type="primary"
              style="font-size: 12px"
              @click="toUser"
              >未有人员？点击创建</el-link
            ></el-col
          >
        </el-row>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="年级状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年级类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择年级类型">
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.GRADE_STATUS)"
              :key="dict.value"
              :value="dict.value"
              :label="dict.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="学制" prop="lengthSchool">
          <el-input v-model="form.lengthSchool" placeholder="请输入学制">
            <template slot="suffix">年</template>
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="上级机构" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入上级机构" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createSchoolGrade,
  updateSchoolGrade,
  deleteSchoolGrade,
  getSchoolGrade,
  getSchoolGradePage,
  exportSchoolGradeExcel,
} from "@/api/system/schoolGrade";
import { listUser } from "@/api/system/user";
export default {
  name: "SchoolGrade",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学校年级信息列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        gradeName: null,
        enrolYear: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gradeName: [
          { required: true, message: "年级名称不能为空", trigger: "blur" },
        ],
        // deptId: [{ required: true, message: "上级机构不能为空", trigger: "blur" }],
      },
      userList: null,
      linkShow: false,
    };
  },
  created() {
    this.getList();
    this.getUserList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getSchoolGradePage(this.queryParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 查询用户列表 */
    getUserList() {
      this.loading = true;
      listUser().then((response) => {
        if (response.data.list && response.data.list.length < 1) {
          this.userList = [
            {
              nickname: "暂无人员，点击创建",
            },
          ];
        } else {
          this.userList = response.data.list;
        }
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        gradeName: undefined,
        enrolYear: undefined,
        inGrade: undefined,
        stage: undefined,
        principal: undefined,
        mobile: undefined,
        status: undefined,
        type: undefined,
        lengthSchool: undefined,
        deptId: this.$store.getters.deptId,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学校年级信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getSchoolGrade(id).then((response) => {
        this.form = response.data;
        this.form.type = this.form.type.toString();
        this.open = true;
        this.title = "修改学校年级信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateSchoolGrade(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createSchoolGrade(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除学校年级信息编号为"' + id + '"的数据项?')
        .then(function () {
          return deleteSchoolGrade(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出所有学校年级信息数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportSchoolGradeExcel(params);
        })
        .then((response) => {
          this.$download.excel(response, "学校年级信息.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    userListChange(e) {
      this.linkShow = e;
    },
    toUser() {
      this.open = false;
      this.reset();
      this.$router.replace("/system/user");
    },
  },
};
</script>
